add_gdal_driver(
  TARGET gdal_JPEGXL
  SOURCES jpegxl.cpp PLUGIN_CAPABLE)
gdal_standard_includes(gdal_JPEGXL)
gdal_target_link_libraries(gdal_JPEGXL PRIVATE JXL::JXL)

cmake_push_check_state(RESET)
set(CMAKE_REQUIRED_LIBRARIES ${JXL_LIBRARIES})
check_function_exists(JxlEncoderFrameSettingsSetOption HAVE_JxlEncoderFrameSettingsSetOption)
check_function_exists(JxlEncoderFrameSettingsCreate HAVE_JxlEncoderFrameSettingsCreate)
check_function_exists(JxlEncoderSetFrameDistance HAVE_JxlEncoderSetFrameDistance)
check_function_exists(JxlEncoderSetFrameLossless HAVE_JxlEncoderSetFrameLossless)
check_function_exists(JxlEncoderSetCodestreamLevel HAVE_JxlEncoderSetCodestreamLevel)
check_function_exists(JxlEncoderInitExtraChannelInfo HAVE_JxlEncoderInitExtraChannelInfo)
check_function_exists(JxlDecoderSetDecompressBoxes HAVE_JXL_BOX_API)
check_function_exists(JxlEncoderSetExtraChannelDistance HAVE_JxlEncoderSetExtraChannelDistance)

# This function has been removed per https://github.com/libjxl/libjxl/commit/b08a704978d5aeaf6fd1e2aee3ae5907a89e1f96
# Testing its presence enables us to know if JxlDecoderGetColorAsEncodedProfile()
# and other functions have JxlPixelFormat* argument that has been removed
check_function_exists(JxlDecoderDefaultPixelFormat HAVE_JxlDecoderDefaultPixelFormat)

cmake_pop_check_state()
if (HAVE_JxlEncoderFrameSettingsSetOption)
  target_compile_definitions(gdal_JPEGXL PRIVATE -DHAVE_JxlEncoderFrameSettingsSetOption)
endif ()
if (HAVE_JxlEncoderFrameSettingsCreate)
  target_compile_definitions(gdal_JPEGXL PRIVATE -DHAVE_JxlEncoderFrameSettingsCreate)
endif ()
if (HAVE_JxlEncoderSetFrameLossless)
  target_compile_definitions(gdal_JPEGXL PRIVATE -DHAVE_JxlEncoderSetFrameLossless)
endif ()
if (HAVE_JxlEncoderSetFrameDistance)
  target_compile_definitions(gdal_JPEGXL PRIVATE -DHAVE_JxlEncoderSetFrameDistance)
endif ()
if (HAVE_JxlEncoderSetCodestreamLevel)
  target_compile_definitions(gdal_JPEGXL PRIVATE -DHAVE_JxlEncoderSetCodestreamLevel)
endif ()
if (HAVE_JxlEncoderInitExtraChannelInfo)
  target_compile_definitions(gdal_JPEGXL PRIVATE -DHAVE_JxlEncoderInitExtraChannelInfo)
endif ()
if (HAVE_JXL_BOX_API)
  target_compile_definitions(gdal_JPEGXL PRIVATE -DHAVE_JXL_BOX_API)
else()
  message(WARNING "This version of libjxl lacks the box API used for EXIF, XMP and georeferencing support. Consider using libjxl from its main branch, post 0.6.1 release.")
endif ()
if (HAVE_JxlEncoderSetExtraChannelDistance)
  target_compile_definitions(gdal_JPEGXL PRIVATE -DHAVE_JxlEncoderSetExtraChannelDistance)
endif ()
if (HAVE_JxlDecoderDefaultPixelFormat)
  target_compile_definitions(gdal_JPEGXL PRIVATE -DHAVE_JxlDecoderDefaultPixelFormat)
endif()
if (GDAL_USE_JXL_THREADS)
  gdal_target_link_libraries(gdal_JPEGXL PRIVATE JXL_THREADS::JXL_THREADS)
  target_compile_definitions(gdal_JPEGXL PRIVATE -DHAVE_JXL_THREADS)
endif ()
